package com.tjetc.view;

import com.tjetc.domain.CustomerMessage;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.MessageService;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

import java.util.Scanner;

/**
 * 客户管理信息界面
 */
public class MessageManageView {
    private static Scanner sc=new Scanner(System.in);
    private static MessageService messageService= ClientProxy.getProxyInstance(MessageService.class);

    public void start() {
        while (true){
            System.out.println("🥰🥰~~~~~~实名认证页面~~~~~~🥰🥰");
            System.out.println("1.实名认证信息注册");
            System.out.println("2.实名认证信息修改");
            System.out.println("3.查看我的认证信息");
            System.out.println("0.返回上一级");
            System.out.println("请输入0~3");
            int i= sc.nextInt();
            switch (i){
                case 1:
                    register();
                    break;
                case 2:
                    update();
                    break;
                case 3:
                    displayOwnMessage();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误请重新输入");
            }

        }
    }

    /**
     * 根据用户id查询
     */
    private void displayOwnMessage() {
        Result result = messageService.getMessageByCustomerId(AppData.customer.getCustomerId());
        //如果查询结果不为空，则打印个人信息
        if (result.getCode()==200){
            CustomerMessage customerMessage =(CustomerMessage) result.getData();
            System.out.println("个人姓名:"+customerMessage.getName()+",手机号:"+customerMessage.getPhone()
                    +",你的驾驶证为:"+customerMessage.getDirving_license());
        }
    }

    /**
     * 用户修改自我信息
     */
    private void update() {
        Result re = messageService.getMessageByCustomerId(AppData.customer.getCustomerId());
        if (re!=null) {
            System.out.println("请输入手机号");
            String phone = "";
            //输入手机号
            while (true) {
                phone = sc.next();
                //判断手机号是否符合规则
                if (!phone.matches("1[35789]\\d{9}")) {
                    System.out.println("请重新输入正确的手机号!");
                } else {
                    //查询该手机号是否已经被注册，如果无，则退出循环
                    if (((Result) messageService.getMessageByPhone(phone)).getCode() != 200) {
                        break;
                    } else {
                        //如果手机号已经被注册，重新输入手机号
                        System.out.println("你这个手机号已经注册了,请换一个");
                    }
                }
            }
            System.out.println("请输入驾驶证");
            String drivingLicense = "";
            while (true) {
                //输入驾驶证
                drivingLicense = sc.next();
                if (!drivingLicense.matches("[1-9][0-9]{17}")) {
                    System.out.println("请重新输入正确的驾驶证信息!");
                } else {
                    //查询是否有该驾驶证
                    if (((Result) messageService.getMessageByDriving(drivingLicense)).getCode() != 200) {
                        break;
                    } else {
                        System.out.println("你这驾驶证偷的吧,请重新输入");
                    }

                }
            }
            CustomerMessage customerMessage = new CustomerMessage();
            customerMessage.setCustomer_id(AppData.customer.getCustomerId());
            customerMessage.setPhone(phone);
            customerMessage.setDirving_license(drivingLicense);
            Result result = messageService.update(customerMessage);
            System.out.println(result.getMsg());
        }else {
            System.out.println("你还没有注册呢");
        }
    }

    /**
     * 用户完善个人信息界面
     */
    private void register() {
        Result result1 = messageService.getMessageByCustomerId(AppData.customer.getCustomerId());
        //判断是否已经注册
        if (result1.getCode() == 200) {
            System.out.println("你都注册完了你还注册");
        } else {
            System.out.println("请输入你的姓名");
            String name = sc.next();
            System.out.println("请输入手机号");
            String phone = "";
            //输入手机号
            while (true) {
                phone = sc.next();
                //判断手机号是否符合规则
                if (!phone.matches("1[35789]\\d{9}")) {
                    System.out.println("请重新输入正确的手机号!");
                } else {
                    //查询该手机号是否注册
                    if (((Result) messageService.getMessageByPhone(phone)).getCode() != 200) {
                        break;
                    } else {
                        System.out.println("你这个手机号已经注册了,请换一个");
                    }
                }
            }
            String drivingLicense = null;
            System.out.println("请输入驾驶证");
            //输入驾驶证
            while (true) {
                drivingLicense = sc.next();
                //判断驾驶证是否符合规则
                if (!drivingLicense.matches("[1-9][0-9]{17}")) {
                    System.out.println("请重新输入");
                } else {
                    //查询是否有该驾驶证
                    if (((Result) messageService.getMessageByDriving(drivingLicense)).getCode() != 200) {
                        break;
                    } else {
                        System.out.println("你这驾驶证偷的吧,请重新输入");
                    }
                }
            }
            CustomerMessage customerMessage = new CustomerMessage();
            customerMessage.setCustomer_id(AppData.customer.getCustomerId());
            customerMessage.setName(name);
            customerMessage.setPhone(phone);
            customerMessage.setDirving_license(drivingLicense);
            Result result = messageService.register(customerMessage);
            System.out.println(result.getMsg());
        }
    }
}
